package com.heima.xianchengchi;

import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class MyExecutor {
    public static void main(String[] args) throws InterruptedException {
        //创建一个默认线程池（Executors），池子中默认是空的newCachedThreadPool()，可容纳int类型的最大值
        //ExecutorService 可帮助我们控制线程池
        ExecutorService executorService = Executors.newCachedThreadPool();
        //submit()方法，会自动帮我们创建线程对象，任务执行完，线程对象归还池子
        executorService.submit(() -> {
            System.out.println(Thread.currentThread().getName() + "在执行");
        });
        Thread.sleep(1000);
        executorService.submit(() -> {
            System.out.println(Thread.currentThread().getName() + "在执行");
        });
        //所有任务全部执行完毕，关闭池子
        executorService.shutdown();
    }
}
